package org.marsdone.fastdfs.clientDemo

import org.csource.common.NameValuePair
import org.csource.fastdfs.ProtoCommon
import org.csource.fastdfs.StorageClient
import org.csource.fastdfs.TrackerClient
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
import org.csource.fastdfs.ClientGlobal

@RunWith(SpringRunner)
@SpringBootTest
class ClientDemoApplicationTests {

	private static final trackerServers = "192.168.56.136:22122,192.168.56.137:22122,192.168.56.139:22122"
	private static final secret_key = "FastDFS1234567890"

	@Test
	void contextLoads() {
		ClientGlobal.initByTrackers(trackerServers)
		println "ClientGlobal.configInfo(): " + ClientGlobal.configInfo()

		def storageClient = new StorageClient(new TrackerClient().getConnection(), null)

		def metaList = new NameValuePair[1]
		metaList[0] = new NameValuePair("fileName", "build.gradle")
		def uploadBytes = new File("build.gradle").bytes
		def result = storageClient.upload_file(uploadBytes, "aaa", metaList)
		println "Upload result:" + result
		assert 2 == result.length

		byte[] downloadBytes = storageClient.download_file(result[0], result[1])

		assert uploadBytes == downloadBytes

		int ts = (System.currentTimeMillis() / 1000)
		def token = ProtoCommon.getToken(result[1], ts, secret_key)
		println "http://192.168.56.137:8888/${result[1]}?token=$token&ts=$ts"
	}

}
